package com.ruyidd.controller;

import com.jacob.activeX.ActiveXComponent;
import com.ruyidd.consts.BartenderConsts;
import com.ruyidd.entity.BartenderParams;
import com.ruyidd.entity.R;
import com.ruyidd.threads.BartenderPrintThread;
import com.ruyidd.utils.BartenderUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.*;

import java.io.File;

@Slf4j
@Controller
@RequestMapping("/bartender")
public class BartenderPrintController {
    @ResponseBody
    @GetMapping("/checkBartenderInstalled")
    public R checkBartenderInstalled() {
        ActiveXComponent btApp = null;
        try{
            btApp = BartenderUtils.getActiveXComponent();
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
        }finally {
            BartenderUtils.closeActiveXComponent(btApp);
        }
        return R.ok("当前电脑已经安装了Bartender");
    }

    @ResponseBody
    @PostMapping("/print")
    public R print(@RequestBody BartenderParams bartenderParams) throws Exception {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        log.info("同步打印参数：" + bartenderParams);
        String btwAbsolutePath = bartenderParams.getPath() + File.separator + bartenderParams.getBtwName();
        File btwFile = new File(btwAbsolutePath);
        if (!btwFile.exists()) {
            throw new RuntimeException("模板文件不存在:" + btwAbsolutePath);
        }
        if (BartenderConsts.DATA_TYPE_EMBEDED.equals(bartenderParams.getDataType())){
            BartenderUtils.embeddedDataPrint(bartenderParams);
        } else {
            BartenderUtils.txtDataPrint(bartenderParams);
        }
        stopWatch.stop();
        log.info("同步打印完成，耗时{}秒", stopWatch.getTotalTimeSeconds());
        return R.ok("打印成功！");
    }

    @ResponseBody
    @PostMapping("/singletonPrint")
    public R singletonPrint(@RequestBody BartenderParams bartenderParams) throws Exception {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        log.info("同步单实例打印参数：" + bartenderParams);
        String btwAbsolutePath = bartenderParams.getPath() + File.separator + bartenderParams.getBtwName();
        File btwFile = new File(btwAbsolutePath);
        if (!btwFile.exists()) {
            throw new RuntimeException("模板文件不存在:" + btwAbsolutePath);
        }
        if (BartenderConsts.DATA_TYPE_EMBEDED.equals(bartenderParams.getDataType())){
            BartenderUtils.embeddedDataPrintSingleton(bartenderParams);
        } else {
            BartenderUtils.txtDataPrintSingleton(bartenderParams);
        }
        stopWatch.stop();
        log.info("同步单实例打印完成，耗时{}秒", stopWatch.getTotalTimeSeconds());
        return R.ok("打印成功！");
    }


    /**
     * 异步打印
     * @param bartenderParams
     * @return
     * @throws Exception
     */
    @ResponseBody
    @PostMapping("/asynchronousPrinting")
    public R asynchronousPrinting(@RequestBody BartenderParams bartenderParams) throws Exception {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        log.info("异步打印参数：" + bartenderParams);
        /**
         * 校验btw模板文件是否存在
         */
        BartenderUtils.checkBtwFileExisted(bartenderParams);
        BartenderPrintThread bartenderPrintThread = new BartenderPrintThread(bartenderParams);
        bartenderPrintThread.start();
        stopWatch.stop();
        log.info("异步打印完成，耗时{}秒", stopWatch.getTotalTimeSeconds());
        return R.ok("已将打印任务发送至打印机队列");
    }
}
